########
# Usage: Rscript cell_rel_cohort_corHeatmap.R HCC
# 传入参数： cohort = 'HCC'
#
########

library(corrplot)
library(RColorBrewer)
library(reshape2)
library(ggplot2)

library(optparse)

option_list <- list(
  make_option("--b", default = "", type = "character", help = "blood mean file"),
  make_option("--t", default = "", type = "character", help = "tissue mean file"),
  make_option("--bo", default = "", type = "character", help = "blood output png file"),
  make_option("--to", default = "", type = "character", help = "tissue output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))


Args = commandArgs()

# path = 'D:\\exo\\905\\submit 6.1\\cell_rel'
# setwd(path)

file.blood = opt$b
file.tissue = opt$t
outfile.blood = opt$bo
outfile.tissue = opt$to
w=7
h=5

### blood cells
data.cells = read.csv(file.blood, sep=',', header = TRUE, row.names=1)
data = round(data.cells, 3)*100
sampleNum = dim(data)[1]
if (sampleNum>20){
  data = data[1:20, ]                ### 只显示前20个样本
}
data = as.data.frame(t(data))
data$`Blood cell` = row.names(data)
data_m = melt(data, id.vars=c("Blood cell"))

pdf(outfile.blood, width=w, height=h)
colors = c(brewer.pal(11,"RdYlBu"), brewer.pal(11,"PiYG"), '#003300')
p = ggplot(data_m, aes(x=variable, y=value)) +
  geom_bar(stat="identity", position="fill", aes(fill=`Blood cell`), width = 0.7) +  
  scale_fill_manual(values=colors)+
  scale_y_continuous(labels = scales::percent)+
  labs(x = "", y = 'Relative Abundance', title = "The Relative Abundance of Top 20 Samples")+
  guides(fill = guide_legend(ncol = 5, bycol = TRUE, override.aes = list(size = 5))) +
  theme(plot.title = element_text(hjust = 0.5), axis.title = element_text(size = 16, face = "bold",color = 'black'))+
  theme(axis.title.y = element_text(face = 'bold',color = 'black',size = 14),
        axis.title.x = element_text(face = 'bold',color = 'black',size = 14,vjust = -1.2),
        axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
        axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 45, hjust = 1), 
        panel.grid = element_blank(),
        legend.position = 'top',
        legend.key.height = unit(0.5,'cm'),
        legend.text = element_text(face = 'bold',color = 'black',size = 9))
print(p)
dev.off()



### tissue cells
data.cells = read.csv(file.tissue, sep=',', header = TRUE, row.names=1)
data = round(data.cells, 3)*100
sampleNum = dim(data)[1]
if (sampleNum>20){
  data = data[1:20, ]                ### 只显示前20个样本
}
data = as.data.frame(t(data))
data$`Tissue cell` = row.names(data)
data_m = melt(data, id.vars=c("Tissue cell"))

pdf(outfile.tissue, width=w, height=h)
colors = c(brewer.pal(11,"RdYlBu"), brewer.pal(11,"PiYG"), '#003300')
p = ggplot(data_m, aes(x=variable, y=value)) +
  geom_bar(stat="identity", position="fill", aes(fill=`Tissue cell`), width = 0.7) +  
  scale_fill_manual(values=colors)+
  scale_y_continuous(labels = scales::percent)+
  labs(x = "", y = 'Relative Abundance', title = "The Relative Abundance of Top 20 Samples")+
  guides(fill = guide_legend(ncol = 5, bycol = TRUE, override.aes = list(size = 5))) +
  theme(plot.title = element_text(hjust = 0.5), axis.title = element_text(size = 16, face = "bold",color = 'black'))+
  theme(axis.title.y = element_text(face = 'bold',color = 'black',size = 14),
        axis.title.x = element_text(face = 'bold',color = 'black',size = 14,vjust = -1.2),
        axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
        axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 45, hjust = 1), 
        panel.grid = element_blank(),
        legend.position = 'top',
        legend.key.height = unit(0.5,'cm'),
        legend.text = element_text(face = 'bold',color = 'black',size = 9))
print(p)
dev.off()